// packageUser/pages/message/complain-detail.js
import MessageService from '../../../service/MessageService';
import { throttle } from '../../../utils/util.js';
import moment from '../../../libs/moment.min.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    complain: {},
    replys: [],
    type: '',
    user: app.globalData.user,
    modalHidden: true,
    compValue: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _ = this;
    const {type, id} = options;
    this.messageService = new MessageService();
    _.setData({ type, id});
    _._loadComplain(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const {id} = this.data;
    this._loadReply(id);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**加载投诉信息 */
  _loadComplain:function(id){
    const _ = this;
    this.messageService.complainById(id).then(function (res) {
      if (res.status == 0) {
        const fileList = res.data.fileList && res.data.fileList.map(f => {
          return f.fileUrl;
        })
        res.data.fileList = fileList;
        _.setData({ complain: res.data });
      }
    })
  },
  /**加载投诉处理记录 */
  _loadReply:function(id){
    const _ = this;
    this.messageService.queryReply(id).then(function (res) {
      if (res.status == 0) {
        const list = res.data && res.data.map(r => {
          const fileList = r.fileList && r.fileList.map(f => {
            return f.fileUrl;
          });
          r.fileList = fileList;
          r.createTime = moment(r.createTime).format('YYYY年MM月DD日');
          return r;
        })
        _.setData({ replys: list });
      }
    });
  },
  /**
   * 跳转到回复页面
   */
  doHandle: throttle(function(e){
    wx.navigateTo({
      url: '/packageUser/pages/message/complain-handle?id=' + e.target.dataset.id
    })
  },1000),
  onTyComp: function(e){
    const _ = this;
    const {id, type} = this.data;
    if(type == 2){
      this.setData({ modalHidden: false })
    }else{
      const param = { complainId: id, type }
      this.messageService.doComp(param).then(function (res) {
        console.log(res);
        if (res.status === 0) {
          _._loadReply(id);
          _._loadComplain(id);
        } else {
          wx.showToast({
            title: res.message,
            duration: 3000
          })
        }
      })
    }
    
  },
  onCompChange: function(e){
    this.setData({compValue: e.detail.value})
  },
  onNoComp: function(){
    this.setData({ modalHidden: true, compValue: null })
  },
  /**同意赔偿 */
  onComp: function () {
    const _ = this;
    const { id, type, compValue} = this.data;
    const param = { complainId: id, type, money: compValue}
    this.messageService.doComp(param).then(function(res){
      console.log(res);
      if(res.status === 0){
        _.setData({ modalHidden: true, compValue: 0 })
        _._loadReply(id);
        _._loadComplain(id);
      }else{
        wx.showToast({
          title: res.message,
          duration: 3000
        })
      }
    })
  }
})